<?php
session_start();
// 未登录则重定向到登陆页面
if (! isset($_SESSION['username'])) {
    header("Location: http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/login.php");
    exit();
};
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></meta>
<title>管理面板</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="js/ajax.in.js"></script>
</head>
<body>
	<h3>管理面板</h3>
	<input type="button" onclick="window.location.href='index.php'"
		value="返回首页"></input>
		<h4>我的头像</h4>
	<div id="himg">
	<img src="<?=$_SESSION['himg']?>" alt="" width="96" height="96"/>
<form method="post" enctype="multipart/form-data" action="upload.php">
<p><input type="file" id="upImg" name="upImg"/></p>
<p><input type="submit" name="Submit" value="上传头像"/><!--  onclick="uphimgButton()" -->
</p>
</form>
	</div>
		<h4>我的留言</h4>
<?php
require './core/conn.php';
require './core/config.php';

if (! isset($_GET['p'])) {
    $p = 1;
} else {
    $p = $_GET['p'] ? $_GET['p'] : 1;
}
$offset = ($p - 1) * $pagesize;
$query_sql = "SELECT * FROM msg WHERE `nickname` = '$_SESSION[username]' ORDER BY id DESC LIMIT  $offset , $pagesize";
$result = mysqli_query($conn, $query_sql);
if (! $result)
    exit('查询数据错误：' . mysqli_error());

// 循环输出当前页显示数据
while (($gb_array = mysqli_fetch_array($result)) != false) {
    echo '我，发表于：', date("Y-m-d H:i:s", $gb_array['createtime']);
    echo ' ID号：', $gb_array['id'], '<br />';
    echo '内容：', nl2br($gb_array['content']), '<br />';
    if (! empty($gb_array['replytime'])) {
        echo '----------------------------<br />';
        echo '最后修改于：', date("Y-m-d H:i:s", $gb_array['replytime']), '<br />';
        echo nl2br($gb_array['reply']), '<p></p>';
    }
    ?>
	<form id="form1" name="form1" method="post" action="editmsg.php">
		<input name="id" type="hidden" value="<?=$gb_array['id']?>" />
		<p>
			<input type="submit" name="submit" value="修改留言" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<a href="editmsg.php?action=delete&id=<?=$gb_array['id']?>"
				onClick="return confirm('确定删除?');">删除留言</a>
		</p>
	</form>
<?php
    echo "<hr />";
}
// 计算留言页数
$count_result = mysqli_query($conn, "SELECT count(*) FROM msg WHERE `nickname` = '$_SESSION[username]'");
$count_array = mysqli_fetch_array($count_result);
$pagenum = ceil($count_array['count(*)'] / $pagesize);
echo '共 ', $count_array['count(*)'], ' 条留言';
if ($pagenum > 1) {
    for ($i = 1; $i <= $pagenum; $i ++) {
        if ($i == $p) {
            echo '&nbsp;[', $i, ']';
        } else {
            echo '&nbsp;<a href="admin.php?p=', $i, '">' . $i . '</a>';
        }
    }
}

// 管理用户
if ($_SESSION['admin'] == '1') {
    echo '<h3>用户管理</h3>';
    $user_result = mysqli_query($conn, "SELECT `username` FROM user");
    $user_array = mysqli_fetch_array($user_result);
    $i = 1;
    echo "<p></p>";
    while (($name_array = mysqli_fetch_array($user_result)) != false) {
        echo $i;
        $i ++;
        echo "&nbsp;";
        echo "<a href='user.php?name=", $name_array['username'], "'>", $name_array['username'], "</a>";
        echo "<p></p>";
    }
}
// 修改密码

?>
</body>
</html>